﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.Collections.Concurrent;
using Microsoft.Office.Interop.Excel;
using SUNRPA.CONTROLS.IMPL.util;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class AddExcelFilterNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            AddExcelFilterNParam? param = item.ControlParam as AddExcelFilterNParam;
            string excelObj = param.ExcelObject;
            
            ExcelModel excelModel = this.GetObjVar<ExcelModel>(ref excelObj, globleData);
            Workbook wb = excelModel.WorkBookValue as Workbook;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", excelObj));
            }

            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);

            string sheetName = param.SheetName;
            this.GetStringVar(ref sheetName, globleData);

            int filterType = param.FilterType;
            if(filterType == 0) {
                foreach(string con in param.Conditions) {
                    string[] nv = con.Split(new string[] {"==="}, StringSplitOptions.RemoveEmptyEntries);
                    excelUtil.SetFilter(sheetName, nv[0], nv[1]);
                }
            } else { 
                excelUtil.RemoveFilter(sheetName);
            }

            return currentIndex;
        }
    }
}
